<?php
require_once("lib/db_connection.php");
$company_id = $_SESSION['company_id'];

$query_flag_default = "SELECT flag_default FROM company_tbl WHERE company_id='".$company_id."'";
$result_flag_default = mysql_query($query_flag_default) or die("SELECT FLAG DEFAULT ".mysql_error());
$row_f_def = mysql_fetch_array($result_flag_default);
$flag_default = $row_f_def['flag_default'];
$get_reset = $_GET['reset'];

if($get_reset==1 || $flag_default==0)
{
$query_del_expense = "DELETE FROM lookup_expense_tbl WHERE company_id='".$company_id."'";
$query_del_fuel = "DELETE FROM lookup_fuel_tbl WHERE company_id='".$company_id."'";
$query_del_inventory = "DELETE FROM lookup_inventory_tbl WHERE company_id='".$company_id."'";
$query_del_personnel = "DELETE FROM lookup_personnel_tbl WHERE company_id='".$company_id."'";
$query_del_tire = "DELETE FROM lookup_tire_tbl WHERE company_id='".$company_id."'";
$query_del_vehicle = "DELETE FROM lookup_vehicle_tbl WHERE company_id='".$company_id."'";
$query_del_workorder = "DELETE FROM lookup_workorder_tbl WHERE company_id='".$company_id."'";

$query_del_models = "DELETE FROM lookup_models_tbl WHERE company_id='".$company_id."'";
//die($query_del_models);
mysql_query($query_del_expense) or die("DELETE EXPENSE".mysql_error());
mysql_query($query_del_fuel) or die("DELETE FUEL ".mysql_error());
mysql_query($query_del_inventory) or die("DELETE INVENTORY ".mysql_error());
mysql_query($query_del_personnel) or die("DELETE PERSONNEL ".mysql_error());
mysql_query($query_del_tire) or die("DELETE TIRE ".mysql_error());
mysql_query($query_del_vehicle) or die("DELETE VEHICLE ".mysql_error());
mysql_query($query_del_workorder) or die("DELETE WORKORDER ".mysql_error());

mysql_query($query_del_models) or die("DELETE MODELS ".mysql_error());

//$query_del_company_workorder = "DELETE FROM company_workorders_tbl WHERE company_id='".$company_id."'";
//mysql_query($query_del_company_workorder) or die("DELETE COMPANY WORKORDER ".mysql_error());


$query_default = "SELECT * FROM default_droplist_tbl";
$result_default = mysql_query($query_default) or die("SELECT DEFAULT ".mysql_error());
while($row_def = mysql_fetch_array($result_default))
{
	$category = $row_def['category'];
	$value = $row_def['value'];

	if($category=='accounts')
	{
		$query_accounts = "INSERT INTO lookup_expense_tbl (company_id, category, value) VALUES
		('".$company_id."','".$category."','".$value."')";
		mysql_query($query_accounts) or die("INSERT ACCOUNTS ".mysql_error());
	}

	if($category=='expense-descriptions')
	{
		$query_desc = "INSERT INTO lookup_expense_tbl (company_id, category, value) VALUES
		('".$company_id."','descriptions','".$value."')";
		mysql_query($query_desc) or die("INSERT DESCRPTIONS ".mysql_error());
	}

	if($category=='expense-type')
	{
		$query_exp_type = "INSERT INTO lookup_expense_tbl (company_id, category, value) VALUES
		('".$company_id."','types','".$value."')";
		mysql_query($query_exp_type) or die("INSERT EXPENSE TYPE ".mysql_error());
	}





	if($category=='brands' || $category=='dealer names' || $category=='octane/types')
	{
		$query_fuel = "INSERT INTO lookup_fuel_tbl (company_id, category, value) VALUES
		('".$company_id."','".$category."','".$value."')";
		mysql_query($query_fuel) or die("INSERT FUEL ".mysql_error());
	}




	if($category=='inventory-manufacturer')
	{
		$query_fuel_manufacturer = "INSERT INTO lookup_inventory_tbl (company_id, category, value) VALUES
		('".$company_id."','manufacturers','".$value."')";
		mysql_query($query_fuel_manufacturer) or die("INSERT FUEL MANUFACTURER ".mysql_error());
	}

	if($category=='storeroom/locations' || $category=='unit of measure')
	{
		$query_inventory = "INSERT INTO lookup_inventory_tbl (company_id, category, value) VALUES
		('".$company_id."','".$category."','".$value."')";
		mysql_query($query_inventory) or die("INSERT INVENTORY ".mysql_error());
	}

	if($category=='inventory-type')
	{
		$query_inventory_type = "INSERT INTO lookup_inventory_tbl (company_id, category, value) VALUES
		('".$company_id."','types','".$value."')";
		mysql_query($query_inventory_type) or die("INSERT INVENTORY TYPE ".mysql_error());
	}




	if($category=='departments' || $category=='driving history types' || $category=='job titles' || $category=='renewals/certifications')
	{
		$query_personnel = "INSERT INTO lookup_personnel_tbl (company_id, category, value) VALUES
		('".$company_id."','".$category."','".$value."')";
		mysql_query($query_personnel) or die("INSERT PERSONNEL ".mysql_error());
	}




	if($category=='tire-manufacturer')
	{
		$query_tire_mnfr = "INSERT INTO lookup_tire_tbl (company_id, category, value) VALUES
		('".$company_id."','manufacturers','".$value."')";
		mysql_query($query_tire_mnfr) or die("INSERT TIRE MNFR ".mysql_error());
	}

	if($category=='sizes' || $category=='wheel sizes')
	{
		$query_tire = "INSERT INTO lookup_tire_tbl (company_id, category, value) VALUES
		('".$company_id."','".$category."','".$value."')";
		mysql_query($query_tire) or die("INSERT TIRE ".mysql_error());
	}

	if($category=='tire-type')
	{
		$query_tire_type = "INSERT INTO lookup_tire_tbl (company_id, category, value) VALUES
		('".$company_id."','types','".$value."')";
		mysql_query($query_tire_type) or die("INSERT TIRE TYPE ".mysql_error());
	}



	if($category=='work locations')
	{
		$query_garage = "INSERT INTO lookup_personnel_tbl (company_id, category, value) VALUES
		('".$company_id."','".$category."','".$value."')";
		mysql_query($query_garage) or die("INSERT GARAGE ".mysql_error());
	}

	if($category=='journal categories' || $category=='body styles')
	{
		$query_vehicle = "INSERT INTO lookup_vehicle_tbl (company_id, category, value) VALUES
		('".$company_id."','".$category."','".$value."')";
		mysql_query($query_vehicle) or die("INSERT VEHICLE ".mysql_error());
	}




	if($category=='descriptions' || $category=='serviced by' || $category=='task descriptions')
	{
		$query_workorder = "INSERT INTO lookup_workorder_tbl (company_id, category, value) VALUES
		('".$company_id."','".$category."','".$value."')";
		mysql_query($query_workorder) or die("INSERT WORKORDER ".mysql_error());
	}

	if($category=='workorder-type')
	{
		$query_workorder_type = "INSERT INTO lookup_workorder_tbl (company_id, category, value) VALUES
		('".$company_id."','type','".$value."')";
		mysql_query($query_workorder_type) or die("INSERT WORKORDER TYPE ".mysql_error());
	}

  if($category=='reminder-type')
	{
		$query_desc = "INSERT INTO lookup_reminder_tbl (company_id, category, value) VALUES
		('".$company_id."','type','".$value."')";
		mysql_query($query_desc) or die("INSERT REMINDER ".mysql_error());
	}


	if($category=='manufacturer')
	{
		$query_veh_mnfr = "INSERT INTO lookup_vehicle_tbl (company_id, category, value) VALUES
		('".$company_id."','".$category."','".$value."')";
		mysql_query($query_veh_mnfr) or die("INSERT VEHICLE MNFR ".mysql_error());
	}
}
	$query_def_mnfr = "SELECT * FROM default_droplist_tbl WHERE category='manufacturer'";
	$result_def_mnfr = mysql_query($query_def_mnfr) or die("SEL OLD MNFR ".mysql_error());
	$ctr=0;
	while($row_def_mnfr = mysql_fetch_array($result_def_mnfr))
	{
		$query_mfnr = "SELECT id FROM lookup_vehicle_tbl WHERE value='".$row_def_mnfr['value']."' AND company_id='".$_SESSION['company_id']."'";
		$result_mnfr = mysql_query($query_mfnr) or die("SELECT MNFR ".mysql_error());
		$row_mnfr = mysql_fetch_array($result_mnfr);
		$new_mnfr_id = $row_mnfr['id'];

		$def_mnfr_id = $row_def_mnfr['auto_id'];
		$query_def_models = "SELECT * FROM default_model_tbl WHERE mnfr_id='".$def_mnfr_id."'";
		$result_def_models = mysql_query($query_def_models) or die("SEL DEF MODELS ".mysql_error());

		while($row_def_models = mysql_fetch_array($result_def_models))
		{
			$model = $row_def_models['value'];
			$query_ins_model = "INSERT INTO lookup_models_tbl (company_id, mnfr_id, model_name) VALUES
			('".$company_id."','".$new_mnfr_id."','".$model."')";
			mysql_query($query_ins_model) or die("INSERT VEHICLE MNFR ".mysql_error());
			$ctr++;

		}
	}

	$query_up_flag = "UPDATE company_tbl SET flag_default='1' WHERE company_id='".$_SESSION['company_id']."'";
	mysql_query($query_up_flag) or die("UPDATE FLAG DEFAULT".mysql_error());
	echo $ctr;
}

/*
$category_array = array('accounts', 'descriptions', 'expense-type',
'brands', 'dealer names', 'octane/types',
'fuel-manufacturer', 'storeroom/locations', 'inventory-type', 'unit of measure',
'departments', 'driving history types', 'job titles', 'renewals/certifications',
'tire-manufacturer', 'sizes', 'tire-type', 'wheel sizes',
'work locations', 'journal categories', 'body styles',
'descriptions', 'serviced by', 'workorder-type');


$tbl_array = array('lookup_expense_tbl', 'lookup_expense_tbl', 'lookup_expense_tbl',
'lookup_fuel_tbl', 'lookup_fuel_tbl', 'lookup_fuel_tbl',
'lookup_inventory_tbl', 'lookup_inventory_tbl', 'lookup_inventory_tbl', 'lookup_inventory_tbl',
'lookup_personnel_tbl', 'lookup_personnel_tbl', 'lookup_personnel_tbl', 'lookup_personnel_tbl',
'lookup_tire_tbl', 'lookup_tire_tbl', 'lookup_tire_tbl', 'lookup_tire_tbl',
'lookup_personnel_tbl', 'lookup_vehicle_tbl', 'lookup_vehicle_tbl',
'lookup_workorder_tbl', 'lookup_workorder_tbl', 'lookup_workorder_tbl');
*/
?>
<html>
<head>
<script type=text/javascript>
function success()
{
	alert('Successfully reset your settings!');
}
</script>
</head>
<body onload=success()>
</body>
</html>